import {defineStore} from 'pinia';

const statusOption = [
    {label: "正常", value: "0"},
    {label: "停用", value: "1"}
]

const API = {
    LIST: '/menu/tree',
    ADD: "/menu/save",
    MODIFY: "/menu/modify",
    INFO: "/menu/get",
    REMOVE: "/menu/remove"
}

const COLUMNS = [
    {field: "name", title: "菜单名称", align: "left"},
    {field: "icon", title: "图标", align: "center", width: "150px"},
    {field: "permission", title: "权限标识", align: "center", width: "250px"},
    {field: "componentPath", title: "组件路径", align: "center", width: "250px"},
    // {field: "status", title: "状态", align: "center", width: "150px"},
    {field: "visible", title: "是否可见", align: "center", width: "150px"},
    // {field: "createTime", title: "创建时间", align: "center", width: "220px"},
]

const SEARCH_FORM = [
    {field: "name", title: "菜单名称", placeholder: "请输入菜单名称"},
    // {field: "status", title: "状态", options: statusOption},
]


const store = {
    id: "assignMenu",
    state(){
        return {
            table: null,
            pageNo: 1,
            pageSize: 10,
            searchForm: {},
            dataList: []
        }
    },

    getters: {

    },

    actions: {
        init(table) {
            this.table = table;
            this.table.setColumns(COLUMNS);
            this.table.setSearchForm(SEARCH_FORM);
        },

        refresh(isFirstPage) {
            if (isFirstPage) {
                this.getList(1, 10)
                return
            }
            this.getList(this.pageNo, this.pageSize)
        },

        /**
         * 列表数据
         * @param condition
         * @returns {Promise<void>}
         */
        async getList(condition) {
            let params = {};
            for (let key in condition) {
                if(! Helper.empty(condition[key])) {
                    params[key] = condition[key];
                }
            }

            Msg.loading("加载中...")
            let {data} = await Http.get(API.LIST, params);
            Msg.loading(false)

            let dataList = []
            if(! Helper.emptyArray(data)) {
                dataList = data
            }
            this.dataList = dataList
            this.table.setData(dataList);
        },
    },
}

export default defineStore(store)
